import {observable, action, computed, toJS} from 'mobx';
import {WeaSelect, WeaTools, WeaLocaleProvider} from 'ecCom';
import StringUtilsStore from './StringUtilsStore'

const getLabel = WeaLocaleProvider.getLabel;

let StringUtils = new StringUtilsStore()

export default class Store {

    @observable
    defaultTableDatas = [
        {
            id: 0,
            title: getLabel(28309, '我的绩效'),
            selectedBigType: "1",
            performanceDateRangeValue2: "",
            orderby: getLabel(19282, "分数") + "-" + getLabel(384984, "降序"),
            showCount: "1"
        },
        {
            id: 1,
            title: getLabel(508776, '所有绩效'),
            selectedBigType: "2",
            performanceDateRangeValue2: "",
            selectedPerformanceStatus: "0,1",
            orderby: getLabel(19282, "分数") + "-" + getLabel(384984, "降序"),
            showCount: "1"
        }
    ]

    selectedTableDataId = ""

    @observable
    contentSettingDialogVisible = false

    @action
    changeContentSettingDialogVisible = (v) => {
        this.contentSettingDialogVisible = v
    }

    contentSettingDialogVisibleMethod = ""

    @action
    setDefaultContentSettingDialog = () => {
        this.tabTitle = ""
        this.selectedBigType = ""
        this.orderby = ""
        this.showCount = "0"
        this.performanceDateRangeValue1 = new Date().getFullYear() + "",
        this.performanceDateRangeValue2 = "", //this.performanceDateRangeValues2Options.length > 0 ? this.performanceDateRangeValues2Options[0].key : "",
        this.performanceDateRangeValue3 = "",
        this.selectedPerformanceStatus = ""
        this.selectedPerformanceType = "2"
        this.performancePersonsStr = ""
        this.selectedSubDepOption = ""
        this.performanceSubDepStr = ""
        this.selectedDepOption = ""
        this.performanceDepStr = ""
    }

    @action
    setContentSettingDialog = (currTableData) => {
        // let currTableData = this.tableDatas[id]
        this.tabTitle = currTableData.title
        this.selectedBigType = currTableData.selectedBigType
        this.orderby = currTableData.orderby || ""
        this.showCount = currTableData.showCount
        this.performanceDateRangeValue1 = currTableData.performanceDateRangeValue1
        this.performanceDateRangeValue2 = currTableData.performanceDateRangeValue2
        this.performanceDateRangeValue3 = currTableData.performanceDateRangeValue3
        this.selectedPerformanceStatus = currTableData.selectedPerformanceStatus
        this.selectedPerformanceType = currTableData.selectedPerformanceType || "2"
        this.performancePersonsStr = currTableData.performancePersonsStr
        this.selectedSubDepOption = currTableData.selectedSubDepOption
        this.performanceSubDepStr = currTableData.performanceSubDepStr
        this.selectedDepOption = currTableData.selectedDepOption
        this.performanceDepStr = currTableData.performanceDepStr
    }

    @observable
    tabTitle = ""                 // tab页标题

    @action
    changeTabTitle = (tabTitle) => {
        this.tabTitle = tabTitle
    }

    @observable
    bigTypeOptions = [          // tab页所属大类别(我的绩效，下属绩效...)
        {
            key: "1",
            selected: false,
            showname: getLabel(28309, '我的绩效')
        },{
            key: "2",
            selected: false,
            showname: getLabel(508776, '所有绩效')
        },{
            key: "3",
            selected: false,
            showname: getLabel(508777, '绩效审批')
        }
    ]

    @observable
    selectedBigType = ""

    @computed
    get showPerformanceStatusSetting () {
        return StringUtils.contains("1,2", this.selectedBigType)
    }

    @computed
    get showPerformanceTypeSetting () {
        return StringUtils.contains("3", this.selectedBigType)
    }

    @computed
    get showPerformancePersonsSetting () {
        return StringUtils.contains("2,3", this.selectedBigType)
    }

    @computed
    get showPerformanceSubDepSetting () {
        return StringUtils.contains("2,3", this.selectedBigType)
    }

    @computed
    get showPerformanceDepSetting () {
        return StringUtils.contains("2,3", this.selectedBigType)
    }

    @action
    changeSelectedBigType = (bigType) => {
        this.bigType = bigType
    }

    @observable
    orderby = ""        // tab页内容列表数据排序方式中文说明

    @action
    changeOrderby = (orderby) => {
        this.orderby = orderby
    }

    @observable
    orderbyTransferDatas = [
        {
            id: 1,
            orderType: "",
            name: getLabel(19282, "分数")
        },{
            id: 2,
            orderType: "",
            name: getLabel(508758, "考核状态")
        }
        // ,{
        //     id: 3,
        //     orderType: "",
        //     name: "考核人员"
        // }
    ]

    @action
    changeOrderbyTransferDatas = (orderbyTransferDatas) => {
        this.orderbyTransferDatas = orderbyTransferDatas
    }

    @observable
    orderbyTransferSelectedKeys = []

    @action
    initOrderbyTransferDatas = () => {
        let selectedKeys = []
        this.orderbyTransferDatas.forEach((d, i) => {
            d.orderType = ""
        })
        this.orderby.split(",").forEach((d, i) => {
            this.orderbyTransferDatas.forEach((dd, ii) => {
                if (d.indexOf(dd.name) != -1) {
                    dd.orderType = d.split("-")[1]
                    selectedKeys.push(dd.id)
                }
            })
        })
        this.changeOrderbyTransferSelectedKeys(selectedKeys)
    }

    @action
    changeOrderbyTransferSelectedKeys = (orderbyTransferSelectedKeys) => {
        this.orderbyTransferSelectedKeys.forEach((d, i) => {
            this.orderbyTransferSelectedKeys[i] = ""
        })
        orderbyTransferSelectedKeys.forEach((d, i) => {
            this.orderbyTransferSelectedKeys[i] = d
        })
    }

    @observable
    orderbyTransferDialogVisible = false

    @action
    changeOrderbyTransferDialogVisible = (orderbyTransferDialogVisible) => {
        this.orderbyTransferDialogVisible = orderbyTransferDialogVisible
    }

    @observable
    showCount = "0"           // tab页是否显示数字

    @action
    changeShowCount = (showCount) => {
        this.showCount = showCount
    }


    @observable
    performanceDateRangeValue1 = "" //new Date().getFullYear() + ""            // 年份选择框的值

    @observable
    changePerformanceDateRangeValue1 = (v) => {
        this.performanceDateRangeValue1 = v
    }

    @observable
    performanceDateRangeValue2 = "" //"2-1"              // 季度，月度选择框的值

    @observable
    changePerformanceDateRangeValue2 = (v) => {
        this.performanceDateRangeValue2 = v
    }

    @observable
    performanceDateRangeValue3 = "" //"2-4-6,2-4-8,2-4-10"          // 季度，月度选择框的联动框的值

    @observable
    changePerformanceDateRangeValue3 = (v) => {
        this.performanceDateRangeValue3 = v
    }

    getPerformanceDateRangeValues1Options () {
        let r = []
        r.push({
            key: "",
            showname: ""
        })
        let year = new Date().getFullYear()
        for (let i = 0; i < 10; i++) {
            let key = (year - i) + ""
            r.push({
                key,
                showname: key
            })
        }
        return r
    }

    @observable
    performanceDateRangeValues2Options = []

    @action
    changePerformanceDateRangeValues2Options = (value) => {
        this.performanceDateRangeValues2Options = value
    }

    @computed
    get getPerformanceDateRangeValues3Com () {
        if (this.performanceDateRangeValue2 == "2-3") {
            return (
                <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@tfvprz`} 
                    options = {[
                        {
                            key: "2-3-",
                            showname: ""
                        },{
                            key: "2-3-1",
                            showname: "1季度"
                        },{
                            key: "2-3-2",
                            showname: "2季度"
                        },{
                            key: "2-3-3",
                            showname: "3季度"
                        },{
                            key: "2-3-4",
                            showname: "4季度"
                        }
                    ]}
                    value = {this.performanceDateRangeValue3}
                    onChange = {(value) => {this.changePerformanceDateRangeValue3(value)}}
                />
            )
        } else if (this.performanceDateRangeValue2 == "2-4") {
            return (
                <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@cm45zq`} 
                    multiple = {true}
                    options = {
                        [
                            {
                                key: "2-4-1",
                                showname: "1月"
                            },{
                                key: "2-4-2",
                                showname: "2月"
                            },{
                                key: "2-4-3",
                                showname: "3月"
                            },{
                                key: "2-4-4",
                                showname: "4月"
                            },{
                                key: "2-4-5",
                                showname: "5月"
                            },{
                                key: "2-4-6",
                                showname: "6月"
                            },{
                                key: "2-4-7",
                                showname: "7月"
                            },{
                                key: "2-4-8",
                                showname: "8月"
                            },{
                                key: "2-4-9",
                                showname: "9月"
                            },{
                                key: "2-4-10",
                                showname: "10月"
                            },{
                                key: "2-4-11",
                                showname: "11月"
                            },{
                                key: "2-4-12",
                                showname: "12月"
                            }
                        ]
                    }
                    value = {this.performanceDateRangeValue3}
                    onChange = {(value) => {this.changePerformanceDateRangeValue3(value)}}
                />
            )
        }
    }

    @observable
    performanceStatusOptions = [       // 绩效状态
        {
            key: "0",
            selected: false,
            showname: getLabel(508778, '考核中')
        },{
            key: "1",
            selected: false,
            showname: getLabel(19134, '审批中')
        },{
            key: "3",
            selected: false,
            showname: getLabel(23774, '已完成')
        },{
            key: "4",
            selected: false,
            showname: getLabel(1979, '未开始')
        },{
            key: "5",
            selected: false,
            showname: getLabel(382750, '已过期')
        }
        // ,{
        //     key: "-1",
        //     selected: false,
        //     showname: getLabel(508779, '无方案')
        // }
    ]

    @observable
    selectedPerformanceStatus = ""

    @action
    changeSelectedPerformanceStatus = (v) => {
        this.selectedPerformanceStatus = v
    }

    @observable
    performanceTypeOptions = [       // 审批类型选项 
        {
            key: "1",
            selected: false,
            showname: getLabel(6106, '考核')
        },{
            key: "2",
            selected: false,
            showname: getLabel(30835, '审批')
        }
    ]

    @observable
    selectedPerformanceType= "2"

    @action
    changeSelectedPerformanceType = (v) => {
        this.selectedPerformanceType = v
    }

    @observable
    performancePersonsStr = ""

    @action
    changePerformancePersonsStr = (ids, names) => {
        ids = ids ? ids : ""
        names = names ? names : ""

        let s = ""
        let idsArr = ids.split(",")
        let namesArr = names.split(",")
        idsArr.forEach((d, i) => {
            s += (d + "-" + namesArr[i] + ",")
        })
        this.performancePersonsStr = s.substring(0, s.length - 1)
    }

    @computed
    get performancePersonsObj () {
        let arr = []
        this.performancePersonsStr && this.performancePersonsStr.split(",").forEach((d, i) => {
            arr.push({
                id: d.split("-")[0],
                name: d.split("-")[1]
            })
        })
        return arr
    }

    @observable
    performanceSubDepOptions = [
        {
            key: "1",
            selected: false,
            showname: getLabel(508780, '含子分部')
        },{
            key: "2",
            selected: false,
            showname: getLabel(18919, '仅本分部') 
        },{
            key: "3",
            selected: false,
            showname: getLabel(18920, '仅子分部')
        }
    ]

    @observable
    selectedSubDepOption = ""

    @action
    changeSelectedSubDepOption = (value) => {
        this.selectedSubDepOption = value
    }

    @observable
    performanceSubDepStr = ""

    @action
    changePerformanceSubDepStr = (ids, names) => {
        ids = ids ? ids : ""
        names = names ? names : ""

        let s = ""
        let idsArr = ids.split(",")
        let namesArr = names.split(",")
        idsArr.forEach((d, i) => {
            s += (d + "-" + namesArr[i] + ",")
        })
        this.performanceSubDepStr = s.substring(0, s.length - 1)
    }

    @computed
    get performanceSubDepObj () {
        let arr = []
        this.performanceSubDepStr && this.performanceSubDepStr.split(",").forEach((d, i) => {
            arr.push({
                id: d.split("-")[0],
                name: d.split("-")[1]
            })
        })
        return arr
    }

    @observable
    performanceDepOptions = [
        {
            key: "1",
            selected: false,
            showname: getLabel(508784, '含子部门') 
        },{
            key: "2",
            selected: false,
            showname: getLabel(18916, '仅本部门') 
        },{
            key: "3",
            selected: false,
            showname: getLabel(18917, '仅子部门')
        }
    ]

    @observable
    selectedDepOption = ""

    @action
    changeSelectedDepOption = (value) => {
        this.selectedDepOption = value
    }

    @observable
    performanceDepStr = ""

    @action
    changePerformanceDepStr = (ids, names) => {
        ids = ids ? ids : ""
        names = names ? names : ""

        let s = ""
        let idsArr = ids.split(",")
        let namesArr = names.split(",")
        idsArr.forEach((d, i) => {
            s += (d + "-" + namesArr[i] + ",")
        })
        this.performanceDepStr = s.substring(0, s.length - 1)
    }

    @computed
    get performanceDepObj () {
        let arr = []
        this.performanceDepStr && this.performanceDepStr.split(",").forEach((d, i) => {
            arr.push({
                id: d.split("-")[0],
                name: d.split("-")[1]
            })
        })
        return arr
    }
}